package com.sat.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sat.goods.mapper.CategoryMapper;
import com.sat.goods.model.Category;
import com.sat.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findList(Category category) {
        QueryWrapper<Category> queryWrapper = getAlbumQueryWrapper(category);
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page findPage(Integer page, Integer size) {
        Page<Category> specPage = new Page<>(page, size);
        return categoryMapper.selectPage(specPage, null);
    }

    @Override
    public Page findPage(Integer page, Integer size, Category category) {
        Page<Category> specPage = new Page<>(page, size);
        QueryWrapper<Category> specQueryWrapper = getAlbumQueryWrapper(category);
        return categoryMapper.selectPage(specPage, specQueryWrapper);
    }

    @Override
    public List<Category> fingByPid(Integer pid) {
        List<Category> categoryList = categoryMapper.fingByPid(pid);
        return categoryList;
    }

    private QueryWrapper<Category> getAlbumQueryWrapper(Category category) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (category != null) {
            if (category.getName() != null && !"".equals(category.getName())) {
                queryWrapper.like("name", category.getName());
            }
        }
        return queryWrapper;
    }
}
